package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * title:
 * author: Gyh
 * date:2023-04-16 14:55
 * Description:
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    //根据多个三级分类的id查询当前分类下平台属性的列表(包含平台属性值列表)
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //获取持久层对象调用持久层自定义查询方法
        BaseAttrInfoMapper baseAttrInfoMapper = this.getBaseMapper();
        //返回结果
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.getAttrInfoList(category1Id,category2Id,category3Id);
        return baseAttrInfoList;
    }

    //保存平台属性和属性列表
    @Transactional(rollbackFor = Exception.class)
        //rollbackFor当事务发生了什么样的异常 才会回滚
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //todo 判断新增平台属性是否存在
        //是否有id提交，如果提交了，说明是修改操作，如果没有提交，则是新增操作
        if (baseAttrInfo.getId() == null){
        //新增操作
            //新增平台属性记录到平台属性base_arttr_info表
                //主键回查机制<SelectKey> SELECT LAST_INSERT_ID()
            this.save(baseAttrInfo);
            //批量新增平台属性值记录到base_arttr_value表
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)){
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存
                baseAttrValueService.saveBatch(attrValueList);
            }
        }else {
            //修改操作
            this.updateById(baseAttrInfo);
            //先删除所有的平台属性值在新增
            LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            baseAttrValueService.remove(queryWrapper);

            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)){
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存
                baseAttrValueService.saveBatch(attrValueList);
            }
        }
    }

    //根据平台属性id 查询平台属性值列表
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAttrValue::getAttrId,attrId);
        return baseAttrValueService.list(queryWrapper);
    }

    /**
     * 根据skuId 获取平台属性数据
     * @param skuId
     * @return
     */
    @Override
    @GmallCache(prefix = "attrList:")
    public List<BaseAttrInfo> getAttrList(Long skuId) {
        //获取持久层对象调用持久层自定义查询方法
        BaseAttrInfoMapper baseAttrInfoMapper = this.getBaseMapper();
        List<BaseAttrInfo> list = baseAttrInfoMapper.getAttrListBySkuId(skuId);
        //前端渲染平台属性通过attrName展示平台属性名称和attrValue展示平台属性值
        list.stream().forEach(baseAttrInfo -> {
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)){
                //sku商品只要新增时，选择一个属性值，最多一个sku有一个平台属性值
                baseAttrInfo.setAttrValue(attrValueList.get(0).getValueName());
            }
        });
        return list;
    }
}
